package com.example2016.cjj.wxplangamedemo.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.example2016.cjj.wxplangamedemo.view.GameView;

/**
 * Created by CJJ on 2017/3/26.
 *
 * 走直线的Sprite类，其位置只能直上直下
 */

public class AutoSprite extends Sprite {
    //每帧移动的像素数，从上往下为正数
    private float speed = 2;


    public AutoSprite(Bitmap bitmap) {
        super(bitmap);
    }

    @Override
    protected void beforeDraw(Canvas canvas, Paint paint, GameView gameView) {
       if( !isDestroy() ){
           //在y轴方向移动speed像素
            move(0, speed*gameView.getDensity() );
       }
    }

    @Override
    protected void afterDraw(Canvas canvas, Paint paint, GameView gameView) {
        //检查Sprite是否超出了Canvas的范围，如果超出，则销毁Sprite
        if( !isDestroy() ){
            RectF canvasRect = new RectF(0,0,canvas.getWidth(),canvas.getHeight());
            RectF spriteRect = getRectf();
            if( !RectF.intersects( canvasRect,spriteRect )){
                destroy();
            }

        }
    }



    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

}
